package com.madrivergroup.mamatodo;

import java.util.List;
import java.util.StringTokenizer;

import com.madrivergroup.mamatodo.adapter.TaskListAdapter;
import com.madrivergroup.mamatodo.tasks.Task;

import static com.madrivergroup.mamatodo.util.MaMaConstants.*;


import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

/**
 * Activity to display the List of Tasks.
 * This is also declared as the main Activity when the application is started
 * 
 * @author peterdelaney
 *
 */
public class ViewTaskActivity extends ListActivity {


	private static final String NAME = ViewTaskActivity.class.getSimpleName();
	
	public static String TASKS = "TASKS";
	public static final String TASK_LIST = "TASK_LIST";
	private MaMaToDoApplication app;
	private TaskListAdapter     adapter;
	private Button              addButton;
	private Button              alterListButton;
	private Button              exitButton;
	private Button              deleteButton;


	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView( R.layout.main);

		setUpViews();

		// Get Application		
		app = (MaMaToDoApplication)getApplication();

		
		// Create an Adapter
		adapter = new TaskListAdapter(app.getCurrentTasks(), this );
		
		// Set the Adaptor on the ListActivity
		setListAdapter(adapter);
	} // end of onCreate()

	/**
	 * Sets up the View
	 */
	private void setUpViews() {
		Log.d(LOG, NAME + ":setUpViews()");
		
		addButton    = (Button)findViewById(R.id.add_task_button);
		alterListButton = (Button)findViewById(R.id.alter_tasks_button);
		deleteButton = (Button)findViewById(R.id.delete_button);		
		exitButton   = (Button)findViewById(R.id.exit_button);


		// Add listener to Button
		addButton.setOnClickListener( new View.OnClickListener()  {
			public void onClick(View view) {
				Log.d(LOG, NAME + ":addButton:onClickView()");				
				Intent intent = new Intent(ViewTaskActivity.this, AddTaskActivity.class);
				startActivity( intent );
			}
		});
		
		// Remove listener to Button does not display completed tasks
		alterListButton.setOnClickListener( new View.OnClickListener()  {
			public void onClick(View view) {
				Log.d(LOG, NAME + ":alterListButton:onClickView()");

				String allString = getString(R.string.list_all_tasks );
				String buttonStr = alterListButton.getText().toString();
				
				// Means we have to Remove the Completed
				if( allString.equals( buttonStr ) ) {
					Log.i(LOG, "removing completed items");
					//List<Task> toRemove = adapter.removeCompletedTasks();
					adapter.forceReload();
				}
				else {
					MaMaToDoApplication app = (MaMaToDoApplication)getApplication();
					List<Task> list = app.getCurrentTasks();
					Log.i(LOG, "Show all items in list size : " + list.size() );					
					//adapter.setTaskList( list );
					adapter.forceReload();					
				}
				changedTextOnButton( buttonStr );				
			}
		});

		
		
		// Delete Tasks Button
		deleteButton.setOnClickListener( new View.OnClickListener()  {
			public void onClick(View view) {
				Log.d(LOG, NAME + ":deleteButton:onClick()");

				// Get list that is Checked that need to be removed
				List<Task> toRemove = adapter.removeCompletedTasks();
				adapter.forceReload();
				
				// Remove from Storage
				MaMaToDoApplication app = (MaMaToDoApplication)getApplication();
				for( Task t : toRemove ) {
					app.removeTask( t );
				}
			}
		});
		
		
		// Exit Application Button
		exitButton.setOnClickListener( new View.OnClickListener()  {
			public void onClick(View view) {
				Log.d(LOG, NAME + ":exitButton:onClick()");
				
				// Move back
				moveTaskToBack(true);
			}
		});
		
	} // end of setUpView()
	

	/**
	 * Change the text of the All/Not Complete button
	 * 
	 */
	protected void changedTextOnButton( String currentText ) {
		Log.d(LOG, NAME + ":changedTextOnButton( " + currentText + " ) ");
		
		String newText = null;
		String allText = getString(R.string.list_all_tasks);
		String notCompleteText = getString(R.string.list_not_complete_tasks);
		if( allText.equals( currentText ) ) {
			newText = notCompleteText;
		}
		else {
			newText = allText;			
		}
		alterListButton.setText(newText);
	} // end of changedTextOnButton()

	/* (non-Javadoc)
	 * @see android.app.ListActivity#onListItemClick(android.widget.ListView, android.view.View, int, long)
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Log.d(LOG, NAME + "onListItem()");
		
		Task task = (Task)adapter.getItem( position);
		task.toggleTask();
		adapter.forceReload();
	}

	/**
	 * Resume will get called when the Add Task Activity is completed
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		Log.d(LOG, NAME + ":onResume()");
		super.onResume();
		adapter.forceReload();		
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onStop()
	 */
	@Override
	protected void onStop() {
		Log.d(LOG, NAME + ":onStop()");		
		super.onStop();
	}


	/**
	 * Method will retrieve the task list from Preferences
	 */
	private void showTasksFromPreferences() {
		Log.d(LOG, NAME + ":showTasksFromPreferences()");
		
		StringBuffer sb  = new StringBuffer();
		
		SharedPreferences   pref  = getSharedPreferences(TASKS, this.MODE_WORLD_READABLE );
		String tasks = pref.getString( TASK_LIST , "");
		StringTokenizer token = new StringTokenizer(tasks, "|");
		while( token.hasMoreElements() ) {
			String task = token.nextToken();
			sb.append( String.format("* %s\n", task));
		}
		
	} // end of showTasksFromPreferences()


} // end of ViewTaskActivity class
